<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="execution">
				<avue-crud
					ref="crud"
					v-model="form"
					v-model:page="page"
					:data="tableData"
					:table-loading="tableLoading"
					:option="tableOption"
					:before-open="handleOpenBefore"
					:before-close="closeDig"
					:permission="permissionList"
					@on-load="getList"
					@search-change="searchChange"
					@search-reset="resetsearch"
					@refresh-change="refreshChange"
					@size-change="sizeChange"
					@current-change="currentChange"
					@row-update="handleUpdate"
					@row-save="handleSave"
					@row-del="rowDel"
				>
					<template #menu-left="{}">
						<el-button  class="filter-item" type="primary" icon="el-icon-edit" @click="$refs.crud.rowAdd()"
							>添加
						</el-button>
						<el-button
							
							class="filter-item"
							plain
							type="primary"
							icon="el-icon-upload"
							@click="$refs.excelUpload.show()"
							>导入
						</el-button>
						<!-- <el-button
							
							class="filter-item"
							plain
							type="primary"
							icon="el-icon-download"
							@click="exportExcel"
							>导出
						</el-button> -->
					</template>
					<template #menu-right="{}">
						<el-button class="filter-item"  v-auth="'access_accessBuildings_export'" circle icon="Download"  @click="exportExcel"></el-button>
					</template>
					<template #buildingsId-form>
						<el-row>
							<el-col :span="6">
								<avue-select :disabled="isDisabled" v-model="sheng" placeholder="请选择省份" :dic="shengdic" @change="shengChange"> </avue-select>
							</el-col>
							<el-col :span="6">
								<avue-select :disabled="isDisabled" v-model="shi" placeholder="请选择市" :dic="shidic" @change="shiChange"> </avue-select>
							</el-col>
							<el-col :span="6">
								<avue-select :disabled="isDisabled" v-model="qu" placeholder="请选择区" :dic="qudic" @change="quChange"> </avue-select>
							</el-col>
							<el-col :span="6">
								<avue-select :disabled="isDisabled" v-model="jie" placeholder="请选择街道" :dic="jiedic" @change="jieChange"> </avue-select>
							</el-col>
						</el-row>
					</template>
					<template #buildingsCx-form>
						<avue-input-number v-model="form.buildingsCx" :precision="0" controls-position="" style="width: 150px" :min="0"> </avue-input-number>
						<span>&emsp;层</span>
					</template>

					<template #buildingsMj-form>
						<avue-input-number v-model="form.buildingsMj" :precision="2" controls-position="" style="width: 150px" :min="0"> </avue-input-number>
						<span>&emsp;m²</span>
					</template>
					<template #buildingsQys-form>
						<avue-input-number v-model="form.buildingsQys" :precision="0" controls-position="" style="width: 150px" :min="0"> </avue-input-number>
						<span>&emsp;个</span>
					</template>
					<template #buildingsCs-form>
						<avue-input-number v-model="form.buildingsCs" :precision="0" controls-position="" style="width: 150px" :min="0"> </avue-input-number>
						<span>&emsp;层</span>
					</template>
					<template #buildingsCn-search>
						<avue-input v-model="searchForm.buildingsCn" placeholder="请输入 建筑中文名称" @keyup.enter="getList"></avue-input>
					</template>
				</avue-crud>

				<excel-upload
					ref="excelUpload"
					title="建筑信息导入"
					url="/admin/accessBuildings/import"
					temp-name="建筑信息.xlsx"
					temp-url="/admin/sys-file/local/file/buildings.xlsx"
					@refreshDataList="handleRefreshChange"
				></excel-upload>
			</div>
		</div>
	</div>
</template>

<script>
import {
	fetchList,
	addObj,
	putObj,
	delObj,
	getAreaDataByCode,
	getBamSBuildingCode,
} from '/@/api/basiciot/area/buildings';
import { tableOption } from '/@/crud/basiciot/area/buildings';

import ExcelUpload from '/@/components/ExcelUpload/index.vue';

import { validatenull } from "@smallwei/avue";

import { getAuthBtnList} from '/@/utils/btnpermissions'
import other from '/@/utils/other';


export default {
	name: 'bambuildings',
	components: { ExcelUpload },
	data() {
		return {
			form: {},
			isDisabled: false,
			sheng: '',
			shengdic: [],
			shengdicprops: {
				label: 'label',
				value: 'value',
			},
			shi: '',
			shidic: [],
			qu: '',
			jie: '',
			qudic: [],
			jiedic: [],
			searchForm: {},
			tableData: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10, // 每页显示多少条
			},
			tableLoading: false,
			tableOption: tableOption,
			isupdate: true,
			openType: 'add'
		};
	},
	computed: {
		permissions() {
			return {
				access_accessBuildings_view : getAuthBtnList('access_accessBuildings_view'),
				access_accessBuildings_add: getAuthBtnList('access_accessBuildings_add'),
				access_accessBuildings_edit: getAuthBtnList('access_accessBuildings_edit'),
				access_accessBuildings_del: getAuthBtnList('access_accessBuildings_del'),
				access_accessFloor_export: getAuthBtnList('access_accessBuildings_export'),
				access_accessFloor_import: getAuthBtnList('access_accessBuildings_import')
			}
		},
		permissionList() {
			return {
				addBtn: getAuthBtnList('access_accessBuildings_add'),
				delBtn: getAuthBtnList('access_accessBuildings_del'),
				editBtn: getAuthBtnList('access_accessBuildings_edit')
			};
		}
	},
	mounted() {
		getAreaDataByCode().then((res) => {
			this.shengdic = res.data || [];
			this.shengdic.forEach((data) => {
				data.label = data.name;
				data.value = data.id;
			});
			console.log(this.shengdic);
		});
	},
	methods: {
		resetsearch() {
			this.searchForm = {};
		},
		shengChange(data) {
			if (data.value) {
				getAreaDataByCode(data.value).then((res) => {
					
					if(!validatenull(res.data)){
						this.shidic = res.data
						this.shidic.forEach((data) => {
							data.label = data.name;
							data.value = data.id;
						});
					} else {
						this.shidic = []
					}
					
				});
				if (this.isupdate) {
					this.shi = '';
					this.qu = '';
					this.form.buildingsId = '';
				}
			}
		},
		shiChange(data) {
			if (data.value) {
				getAreaDataByCode(data.value).then((res) => {
					if(!validatenull(res.data)){
						this.qudic = res.data;
						this.qudic.forEach((data) => {
							data.label = data.name;
							data.value = data.id;
						})
					} else {
						this.qudic = []
					}
					
				});
				if (this.isupdate) {
					this.qu = '';
					this.form.buildingsId = '';
				}
			}
		},
		quChange(data) {
			if (data.value) {
				getAreaDataByCode(data.value).then((res) => {
					if(!validatenull(res.data)){
						this.jiedic = res.data;
						this.jiedic.forEach((data) => {
							data.label = data.name;
							data.value = data.id;
						})
					} else {
						this.jiedic = []
						// if(this.qudic && this.qudic[0].areaType == 4) {
						// 	if(this.openType == 'add') {
						// 		getBamSBuildingCode({
						// 			code: data.value,
						// 			level: '2',
						// 			suffix: '01',
						// 		}).then((res) => {
						// 			this.form.buildingsId = res.data;
						// 		});
						// 	}
						// }
					}
					
				});
				if (this.isupdate) {
					this.jie = '';
					this.form.buildingsId = '';
				}
			}
		},
		jieChange(data) {
			if (data.value) {
				if (this.isupdate) {
					getBamSBuildingCode({
						code: data.value,
						level: '2',
						suffix: '01',
					}).then((res) => {
						console.log(res.data.data);
						this.form.buildingsId = res.data;
					});
				}
				this.isupdate = true;
			}
		},
		getList(page, params) {
			this.tableLoading = true;
			fetchList(
				Object.assign(
					{
						current: page.currentPage,
						size: page.pageSize,
					},
					params,
					this.searchForm
				)
			)
				.then((response) => {
					this.tableData = response.data.records;
					this.page.total = response.data.total;
					this.tableLoading = false;
				})
				.catch(() => {
					this.tableLoading = false;
				});
		},
		rowDel: function (row, index) {
			this.$confirm('是否确认删除' + row.buildingsCn, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delObj(row.buildingsId);
				})
				.then((data) => {
					this.$message.success('删除成功');
					this.getList(this.page);
				})
				.catch((cancelorerror) => {});
		},
		handleUpdate: function (row, index, done, loading) {
			putObj(row)
				.then((data) => {
					if (data.data) {
						this.$message.success('修改成功');
					} else {
						this.$message.error('修改失败');
					}

					done();
					this.getList(this.page);
				})
				.catch(() => {
					loading();
				});
		},
		handleSave: function (row, done, loading) {
			if(this.qudic[0].areaType == 4) {
				row.adCode = this.qu
			} else {
				row.adCode = this.jie
			}
			
			addObj(row)
				.then((data) => {
					if (data.data) {
						this.$message.success('添加成功');
					} else {
						this.$message.success('添加失败');
					}

					done();
					this.getList(this.page);
					this.clearData();
				})
				.catch(() => {
					loading();
				});
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize;
		},
		currentChange(current) {
			this.page.currentPage = current;
		},
		searchChange(form, done) {
			// this.searchForm = form
			this.page.currentPage = 1;
			this.getList(this.page, form);
			done();
		},
		refreshChange() {
			this.getList(this.page);
		},
		handleRefreshChange() {
			this.getList(this.page);
		},
		exportExcel() {
			
			other.downBlobFile(other.adaptationUrl('/admin/accessBuildings/export'), this.searchForm, '建筑信息.xlsx');
		},
		closeDig(done) {
			this.clearData();
			done();
		},
		handleOpenBefore(show, type) {
			this.isDisabled = false;
			this.openType = type
			if (type == 'edit') {
				this.isDisabled = true;
				this.isupdate = false;
				if (this.form.buildingsId) {
					this.sheng = this.form.provinceCode;
					// getAreaDataByCode(this.sheng).then((res) => {
					// 	if(!validatenull(res.data)){
					// 		this.shidic = res.data;
					// 		this.shidic.forEach((data) => {
					// 			data.label = data.value;
					// 			data.value = data.code;
					// 		});
					// 	} else {
					// 		this.shidic = []
					// 	}
						
					// });
					// 直辖市 区 街道 重新赋值
					if(!validatenull(this.form.cityCode)){
						this.shi = this.form.cityCode;
						this.qu = this.form.districtCode;
						this.jie = this.form.streetCode;
					} else {
						this.shi = this.form.districtCode;
						this.qu = this.form.streetCode;
						this.jie = ''
					}
					
					// getAreaDataByCode(this.shi).then((res) => {
					// 	if(!validatenull(res.data)){
					// 		this.qudic = res.data;
					// 		this.qudic.forEach((data) => {
					// 			data.label = data.value;
					// 			data.value = data.code;
					// 		});
							
					// 	}else {
					// 		this.qudic = []
					// 	}
					// });
					
					// getAreaDataByCode(this.qu).then((res) => {
					// 	if(!validatenull(res.data)){
					// 		this.jiedic = res.data;
					// 		this.jiedic.forEach((data) => {
					// 			data.label = data.name;
					// 			data.value = data.id;
					// 		})
					// 	} else {
					// 		this.jiedic = []
					// 	}
					// });
				}
			}
			show();
		},
		clearData() {
			this.sheng = '';
			this.qu = '';
			this.qudic = [];
			this.shi = '';
			this.shidic = [];
		},
	},
};
</script>
